<template>
  <view class="avatar-popup">
<!--    <image-->
<!--      class="close-icon"-->
<!--      :src="ossImg('funCard/user_close.png')"-->
<!--      @click.stop="emits('close')"-->
<!--    />-->
    <view
      class="avatar-bg"
      :style="{
        backgroundImage: `url(${ossImg('others/oreo-destiny/bg.png')})`
      }"
    >
      <view class="avatar_body">
        <button
          open-type="chooseAvatar"
          @chooseavatar="onChooseAvatar"
          class="img_btn"
        >
          <view
            class="img"
            :style="{ backgroundImage: `url(${avatarUrl})` }"
          />
        </button>
        <view
          class="set"
          :style="{ backgroundImage: `url(${ossImg('others/oreo-destiny/set_img.png')})` }"
        ></view>
      </view>
    </view>
    <view
      class="name_body flex"
      :style="{
        backgroundImage: `url(${ossImg('others/oreo-destiny/use_nickname.png')})`
      }"
    >
      <image class="w-42px h-20px" :src="ossImg('others/oreo-destiny/nickname.png')" />
      <view class="flex-1" style="width: 100%">
        <input
          class="name_input"
          type="nickname"
          v-model="nickName"
          :class="{ 'is-input': isInput }"
          @focus="nameFocus()"
          @blur="nameChange($event)"
          placeholder="请输入"
        />
      </view>
    </view>
    <image
      class="w-196px h-55px mx-auto block"
      :src="ossImg('others/oreo-destiny/submit.png')"
      @click.stop="setUserInfo"
    />
  </view>
</template>

<script setup>
import { defineProps, ref, defineEmits, watch, onMounted } from "vue";
import { message, ossImg } from "@/utils/utils";
import { updateInfo } from "@/services/biscuit";

const emits = defineEmits(["confirm", "close", "skuChange"]);
const props = defineProps({
  userInfo: {
    type: Object,
    default: () => ({}),
  },
});
const isInput = ref(false)
const nickName = ref("");
const avatarUrl = ref("");
const emdlz = getApp().emdlz2

onMounted(() => {
  console.log(wx.requirePrivacyAuthorize)
  wx.requirePrivacyAuthorize({
    success: () => {
      // 用户同意授权
      // 继续小程序逻辑
    },
    fail: () => {}, // 用户拒绝授权
    complete: () => {}
  })
})

watch(
  () => props.userInfo,
  () => {
    nickName.value = props.userInfo.nickname;
    avatarUrl.value =
      props.userInfo.icon || ossImg("personal/moren_avatar.png");
  },
  {
    immediate: true, // 这个属性是重点啦
  }
);

const onChooseAvatar = (e) => {
  uni.showLoading({ title: "上传中" });
  uni.uploadFile({
    url: process.env.BASE_API + "s-content/api/material/upload", //服务器地址
    fileType: "image",
    filePath: e.detail.avatarUrl,
    name: "file",
    formData: {},
    header: {
      "content-type": "multipart/form-data",
      Authorization: `Bearer ${uni.getStorageSync("TOKEN")}`,
    },
    success: (uploadFileRes) => {
      avatarUrl.value = JSON.parse(uploadFileRes.data).data;
    },
    complete: (res) => {
      uni.hideLoading();
    },
  });

};
const nameFocus = () => {
  isInput.value = true
  emdlz('track','event','Oreo_destiny_authorization_page','click','nickname_authorization');
};
const nameChange = (e) => {
  isInput.value = false
  if (e.detail.value === "") return
  nickName.value = e.detail.value;
};

const setUserInfo = () => {
  emdlz('track','event','Oreo_destiny_authorization_page','click','authorization_confirm');
  return new Promise(async (resolve, reject) => {
    const res = await updateInfo({
      nickname: nickName.value,
      icon: avatarUrl.value,
    });
    if (res?.code === 200) {
      resolve(res);
      emits("close");
    } else {
      message(res.message);
      reject(res.message);
    }
  });
};
</script>

<style lang="less" scoped>
@import "@/static/css/theme.less";

.avatar-popup {
  position: relative;
  min-height: 300px;
  font-weight: 500;
  padding: 27px 0 20px 0;
  .close-icon {
    position: absolute;
    top: 20px;
    left: 21px;
    width: 14px;
    height: 14px;
  }
  .title,
  .text {
    display: block;
    padding-left: 30px;
  }
  .title {
    height: 16px;
    line-height: 16px;
    font-size: 16px;
    color: #212121;
    margin-bottom: 5px;
  }
  .text {
    font-size: 11px;
    line-height: 11px;
    color: #7f7f7f;
  }
  .avatar-bg {
    width: 375px;
    height: 224px;
    background-repeat: no-repeat;
    background-position: top left;
    background-size: 100% 100%;
    display: flex;
    justify-content: center;
    align-items: flex-end;
  }
  .avatar_body {
    position: relative;
    width: 140px;
    height: 140px;
    border-radius: 50%;
    overflow: hidden;
    border: 4px solid #000000;
    .img_btn {
      width: inherit;
      height: inherit;
      padding: 0;
      border-radius: 50%;
      &::after {
        content: none;
      }
    }
    .img {
      width: inherit;
      height: inherit;
      border-radius: 50%;
      background-repeat: no-repeat;
      background-position: top;
      background-size: cover;
    }
    .set {
      position: absolute;
      left: 0;
      bottom: 0;
      z-index: 2;
      background-repeat: no-repeat;
      background-position: center 11px;
      background-size: 44px 20px;
      width: 100%;
      height: 44px;
      background-color: rgba(0, 0, 0, .4);
    }
  }
  .name_body {
    width: 310px;
    height: 33px;
    border-bottom: 3px solid #000000;
    box-sizing: border-box;
    padding: 0 4px 3px 4px;
    display: flex;
    align-items: center;
    margin: 43px auto 16px auto;
    background-size: 92px 16px;
    background-position: right center;
    background-repeat: no-repeat;
    image {
      display: block;
    }
    .name_input {
      width: 100%;
      flex: 1;
      height: 30px;
      line-height: 30px;
      padding: 0 10px 0 17px;
      font-size: 18px;
      font-weight: 400;
      background-color: transparent;
      box-sizing: border-box;
      &.is-input {
        padding-right: 100px;
      }
    }
  }
  .btn {
    position: absolute;
    left: 16px;
    bottom: 9px;
    width: 343px;
    height: 40px;
    line-height: 40px;
    text-align: center;
    background: #0044c9;
    border-radius: 20px;
    font-size: 16px;
    color: #fff;
    &.disabled {
      background-color: #f6f6f6;
      color: #adadad;
    }
  }
}
</style>
